﻿using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.PCI9016.Logic
{
    public class AxitHardware : IAxis
    {
        private Axis _axit;
        public AxitHardware(Axis axis)
        {
            _axit = axis;
        }

        public string ClearCEnc()
        {
            throw new NotImplementedException();
        }

        public string ClearCPos()
        {
            throw new NotImplementedException();
        }

        public string ContinuousMotion(bool bl)
        {
            int error = PCI9016.p9016_set_t_profile(_axit.AxisID, _axit.Parameter.Jog_Min_Vel, _axit.Parameter.Jog_Max_Vel, _axit.Parameter.Jog_Tacc, _axit.Parameter.Jog_Tdec);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_set_t_profile ==>> {(PCI9016Error)error}");
            }
            error = PCI9016.p9016_vmove(_axit.AxisID, bl ? 1 : 0, 0);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_vmove ==>> {(PCI9016Error)error}");
            }
            return "ok";
        }
        public AxisParameter GetAxisParameter() => _axit.Parameter;
        public AxisStatus GetAxisStatus() => _axit.Status;

        public string HomeMove()
        {
            int error = PCI9016.p9016_home_config(_axit.AxisID, _axit.Parameter.Mode, 0, 0);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_home_config ==>> {(PCI9016Error)error}");
            }
            error = PCI9016.p9016_set_t_profile(_axit.AxisID, _axit.Parameter.Low_Vel, _axit.Parameter.High_Vel, _axit.Parameter.Tacc, _axit.Parameter.Tdec);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_set_t_profile ==>> {(PCI9016Error)error}");
            }
            //1-正向，0-反向
            error = PCI9016.p9016_home_move(_axit.AxisID, _axit.Parameter.Home_dir ? 1 : 0);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_home_move ==>> {(PCI9016Error)error}");
            }
            return "ok";
        }

        public string Move(long dist, int dist_mode, int vel_mode)
        {
            int error = PCI9016.p9016_pmove(_axit.AxisID, (int)dist, dist_mode, vel_mode);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_pmove ==>> {(PCI9016Error)error}");
            }
            return "ok";
        }

        public string SetProfile()
        {
            int error = PCI9016.p9016_set_pls_outmode(_axit.AxisID, _axit.Parameter.OutMode);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_set_pls_outmode ==>> {(PCI9016Error)error}");
            }
            if (_axit.AxisID < 4)
            {
                error = PCI9016.p9016_set_pls_iptmode(_axit.AxisID, _axit.Parameter.IptMode);
                if (error != 0)
                {
                    throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_set_pls_iptmode ==>> {(PCI9016Error)error}");
                }
            }
            error = PCI9016.p9016_set_t_profile(_axit.AxisID, _axit.Parameter.Min_Vel, _axit.Parameter.Max_Vel, _axit.Parameter.STacc, _axit.Parameter.STdec);
            if (error != 0)
            {
                throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_set_t_profile ==>> {(PCI9016Error)error}");
            }
            return "ok";
        }

        public string SetServo(bool enable)
        {
            int error = PCI9016.p9016_set_do_bit(_axit.AxisID, 7, (uint)(enable ? 1 : 0));
            if (error == 0)
            {
                return "ok";
            }
            throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_stop ==>> {(PCI9016Error)error}");
        }

        public string Stop(int emgStop)
        {
            int error = PCI9016.p9016_stop(_axit.AxisID, emgStop);
            if (error == 0)
            {
                return "ok";
            }
            throw new Exception($"CardNo[{_axit.CardNo}] Index[{_axit.AxisID}] p9016_stop ==>> {(PCI9016Error)error}");
        }
    }
}
